// const express = require("express");
const express = require("./like-express");
const app = express();

app.use((req, res, next) => {
  console.log("请求开始 ...", req.method, req.url);
  next();
});
app.use((req, res, next) => {
  console.log("cookie ...");
  next();
});
app.use((req, res, next) => {
  console.log("session ...");
  setTimeout(() => next());
});
app.use("/api", (req, res, next) => {
  console.log("api /api 路由");
  next();
});
app.get("/api", (req, res, next) => {
  console.log("get /api 路由");
  next();
});
app.post("/api", (req, res, next) => {
  console.log("post /api 路由");
  next();
});

// 模拟登录验证
function loginCheck(req, res, next) {
  console.log("登陆成功");
  setTimeout(() => {
    next(); // 登陆成功
  });
}

app.get("/api/get-cookie", loginCheck, (req, res, next) => {
  res.json({
    errno: 0,
    data: req.cookies || "cookies",
  });
});

app.post("/api/get-post-data", loginCheck, (req, res, next) => {
  res.json({
    errno: 0,
    data: req.body,
  });
});

// app.use((req, res, next) => {
//   console.log("处理 404");
//   res.json({
//     errno: -1,
//     msg: "404 not fount",
//   });
// });

app.listen(8001, () => {
  console.log("server is running at http://127.0.0.1:8000");
});
